package top.wardyan.wdev.dbdevassist.action;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import top.wardyan.action.base.AbstractShell;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.action.proc.ProcShell;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.YTUser;

public class LogonShell extends AbstractShell<LogonShell>{
	private Composite comMain;
	private Label lblAccount;
	private Label lblPwd;
	private Text txtAccount;
	private Text txtPwd;
	private Button btnLogon;
	private Button chkRemember;
	private Button chkAutologon;

	
	public LogonShell() {
		super(SWT.CLOSE);
		this.createContent();
	}
	
	
	private void createContent() {
		this.setSize(600, 480);
		//this.setLayout(new FormLayout());
//		FormData fd_comMain = new FormData();
//		fd_comMain.bottom = new FormAttachment(this, 350);
//		fd_comMain.top = new FormAttachment(this, 100);
//		fd_comMain.left = new FormAttachment(this, 50);
//		fd_comMain.right = new FormAttachment(100, -80);
//		comMain.setLayoutData(fd_comMain);
		
		
		
		comMain = new Composite(this, SWT.NONE);
		comMain.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		comMain.setLocation(150, 100);
		comMain.setSize(new Point(300, 150));
		
		lblAccount = new Label(comMain, SWT.RIGHT);
		lblAccount.setBounds(10, 34, 62, 20);
		lblAccount.setText("账号");
		
		lblPwd = new Label(comMain, SWT.RIGHT);
		lblPwd.setText("密码");
		lblPwd.setBounds(10, 70, 62, 20);
		
		txtAccount = new Text(comMain, SWT.BORDER);
		txtAccount.setTextLimit(32);
		txtAccount.setBounds(78, 29, 200, 30);
		
		txtPwd = new Text(comMain, SWT.BORDER);
		txtPwd.setTextLimit(32);
		txtPwd.setBounds(78, 65, 200, 30);
		
		btnLogon = new Button(comMain, SWT.NONE);
		btnLogon.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				logon();
			}
		});
		btnLogon.setBounds(191, 101, 87, 34);
		btnLogon.setText("登录");
		
		
		this.setDefaultButton(btnLogon);
		
		chkRemember = new Button(comMain, SWT.CHECK);
		chkRemember.setBounds(10, 108, 87, 21);
		chkRemember.setText("记住密码");
		
		chkAutologon = new Button(comMain, SWT.CHECK);
		chkAutologon.setText("自动登录");
		chkAutologon.setBounds(103, 108, 87, 21);
		DDA dda = DDA.getInstance(); 
		String username = dda.PROP.getProperty("un", "");
		String pwd = dda.PROP.getProperty("p", "");
		String autologon = dda.PROP.getProperty("autologon", "");
		String remember = dda.PROP.getProperty("remember", "");
		chkRemember.setSelection(remember.equals("true"));
		chkAutologon.setSelection(autologon.equals("true"));
		if(remember.equals("true") && username != null && username.length() > 0 && pwd != null && pwd.length() > 0) {
			txtAccount.setText(username);
			txtPwd.setText(pwd);
			chkRemember.setSelection(true);
			if(autologon.equals("true")){
				logon();
			}
		}
		
		ShellUtil.center(this);
	}
	
	private void logon() {
		if(txtAccount.getText().length() < 1) {
			Msg.show("错误", "账号不能为空");
			txtAccount.setFocus();
			return;
		}
		if(txtPwd.getText().length() < 1) {
			Msg.show("错误", "密码不能为空");
			txtPwd.setFocus();
			return;
		}
		YTUser user = DDA.getInstance().userService.find(txtAccount.getText(), txtPwd.getText());
		if(user != null) {
			Msg.show("登录成功", "登录成功,欢迎使用系统");
			DDA.user = user;
			Properties p = DDA.PROP;
			if(chkRemember.getSelection()) {
				p.put("un", user.getXAccount());
				p.put("p", user.getXPassword());
			}else {
				p.put("un", "");
				p.put("p", "");
			}
			p.setProperty("remember", Boolean.valueOf(chkRemember.getSelection()).toString());
			p.setProperty("autologon", Boolean.valueOf(chkAutologon.getSelection()).toString());
			try {
				p.store(new FileWriter(DDA.APPCONFIGFILE), "");
			} catch (IOException e) {
				e.printStackTrace();
			}
			self.dispose();
			new ProcShell().go();
		}else {
			Msg.show("登录失败", "登录失败,请检查输入是否正确");
			txtPwd.setFocus();
		}
	}
	
	public static void main(String[] args) {
		new LogonShell().go();
	}

	@Override
	protected String getTitle() {
		return "登录";
	}
}
